package com.ruoyi.applet.service.impl;

import com.ruoyi.applet.domain.AppletExam;
import com.ruoyi.applet.domain.AppletExamTopic;
import com.ruoyi.applet.mapper.AppletExamMapper;
import com.ruoyi.applet.mapper.AppletExamTopicMapper;
import com.ruoyi.applet.mapper.AppletExamUserMapper;
import com.ruoyi.applet.service.IAppletExamService;
import com.brisk.common.core.domain.AjaxResult;
import com.brisk.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * examService业务层处理
 *
 * @author PengFei Deng
 * @date 2022-04-14
 */
@Service
public class AppletExamServiceImpl implements IAppletExamService {
    @Autowired
    private AppletExamMapper appletExamMapper;


    @Autowired
    private AppletExamTopicMapper appletExamTopicMapper;


    @Autowired
    private AppletExamUserMapper appletExamUserMapper;

    /**
     * 查询exam
     *
     * @param examId exam主键
     * @return exam
     */
    @Override
    public AppletExam selectAppletExamByExamId(Integer examId) {
        AppletExamTopic appletExamTopic = new AppletExamTopic();
        appletExamTopic.setExamId(Long.valueOf(examId));
        List<AppletExamTopic> topics = appletExamTopicMapper.selectAppletExamTopicList(appletExamTopic);
        AppletExam appletExam = appletExamMapper.selectAppletExamByExamId(examId);
        appletExam.setTopicList(topics);
        return appletExam;
    }

    @Override
    public AppletExam selectAppletExamBySiteId(Long siteId) {
        return appletExamMapper.selectAppletExamBySiteId(siteId);
    }

    /**
     * 查询exam列表
     *
     * @param appletExam exam
     * @return exam
     */
    @Override
    public List<AppletExam> selectAppletExamList(AppletExam appletExam) {
        List<AppletExam> examList = appletExamMapper.selectAppletExamList(appletExam);
        if (examList.size() > 0) {
            for (AppletExam exam : examList) {
                AppletExamTopic appletExamTopic = new AppletExamTopic();
                appletExamTopic.setExamId(exam.getExamId());
                List<AppletExamTopic> topics = appletExamTopicMapper.selectAppletExamTopicList(appletExamTopic);
                exam.setTopicList(topics);
            }
        }
        return examList;
    }

    /**
     * 新增exam
     *
     * @param appletExam exam
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertAppletExam(AppletExam appletExam) {
        //查询坐标点是否有了试题
        AppletExam select = new AppletExam();
        select.setSiteId(appletExam.getSiteId());
        List<AppletExam> examList = appletExamMapper.selectAppletExamList(select);
        if (examList.size() > 0) return AjaxResult.error("改坐标点已经有了试题，不可再创建新的试题！");
        appletExam.setCreateTime(DateUtils.getNowDate());
        appletExam.setPeopleSum(0);
        appletExam.setState("1");
        int res = appletExamMapper.insertAppletExam(appletExam);
        if (res < 1) {
            return AjaxResult.error("新增失败");
        }
        List<AppletExamTopic> topics = appletExam.getTopicList();
        for (AppletExamTopic topic : topics) {
            topic.setExamId(appletExam.getExamId());
            appletExamTopicMapper.insertAppletExamTopic(topic);
        }
        return AjaxResult.success();
    }

    /**
     * 修改exam
     *
     * @param appletExam exam
     * @return 结果
     */
    @Override
    public int updateAppletExam(AppletExam appletExam) {
        appletExam.setUpdateTime(DateUtils.getNowDate());
        List<AppletExamTopic> topics = appletExam.getTopicList();
        if (topics != null) {
            for (AppletExamTopic topic : topics) {
                if (null == topic.getTopicId()) { // 如果没有topicId 就代表是新增的
                    topic.setExamId(appletExam.getExamId());
                    appletExamTopicMapper.insertAppletExamTopic(topic);
                } else {
                    appletExamTopicMapper.updateAppletExamTopic(topic);
                }
            }
        }
        return appletExamMapper.updateAppletExam(appletExam);
    }

    /**
     * 批量删除exam
     *
     * @param examIds 需要删除的exam主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteAppletExamByExamIds(Integer[] examIds) {
        for (Integer examId : examIds) {
            appletExamTopicMapper.deleteByExamId(examId);
            appletExamUserMapper.deleteAppletExamUserByExamId(Long.valueOf(examId));
        }


        return appletExamMapper.deleteAppletExamByExamIds(examIds);
    }

    /**
     * 删除exam信息
     *
     * @param examId exam主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteAppletExamByExamId(Integer examId) {
        appletExamTopicMapper.deleteByExamId(examId);
        return appletExamMapper.deleteAppletExamByExamId(examId);
    }

    @Override
    public List<AppletExam> appletSelect(AppletExam appletExam) {
        return appletExamMapper.appletSelectList(appletExam);
    }

    @Override
    public AjaxResult appletGetByExamId(Integer examId, Integer isAnswer) {
        AppletExam exam = appletExamMapper.selectAppletExamByExamId(examId);
        List<AppletExamTopic> topicList = new ArrayList<>();
        if (isAnswer == 1) {
            topicList = appletExamTopicMapper.appletGetByExamIdWithAnswer(examId);
        } else {
            topicList = appletExamTopicMapper.appletGetByExamIdWithoutAnswer(examId);
        }
        for (AppletExamTopic appletExamTopic : topicList) {
            List<String> choose = new ArrayList<>();
            choose.add(appletExamTopic.getChoose1());
            choose.add(appletExamTopic.getChoose2());
            choose.add(appletExamTopic.getChoose3());
            choose.add(appletExamTopic.getChoose4());
            appletExamTopic.setChoose(choose);
        }
        exam.setTopicList(topicList);
        return AjaxResult.success(exam);
    }
}
